package com.bree.erp.jsf.view;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.bree.erp.entity.ItemRequest;
import com.bree.erp.entity.PartUnit;
import com.bree.erp.jsf.datatable.LazyBreeDataModel;
import com.bree.erp.jsf.httpsess.SessionDataKey;
import com.bree.erp.jsf.httpsess.UserSessionData;
import com.bree.erp.service.ItemRequestService;
import com.bree.erp.service.ItemRequestWrapperService;
import com.bree.erp.service.wrapper.ItemRequestWrapperServiceBean;

@ManagedBean(name="dtItemRequestLazyView")
@ViewScoped
public class ItemRequestLazyView {

	private static Logger LOG = Logger.getLogger(ItemRequestLazyView.class);

	@ManagedProperty(value="#{userSession}")
	private UserSessionData userSession;

    private LazyDataModel<ItemRequest> lazyModel;
    
    @EJB
    private ItemRequestService irService;
    
    private ItemRequestWrapperService irWrapperService;
    
    private ItemRequest selectedItem;
    
    private double quantity;
    
    private List<Long> forDeletion;
    
    @PostConstruct
    public void init() {
    	Long prId = userSession.get(SessionDataKey.SELECTED_PURCHASE_REQUISITION_ID, Long.class);
    	irWrapperService = new ItemRequestWrapperServiceBean(irService, prId);
    	lazyModel = new LazyBreeDataModel<ItemRequest, ItemRequestWrapperService, Long>(irWrapperService, 0L);
    	
    	initForDeletion();
    }
    
    public void initForDeletion() {
    	if(forDeletion == null) {
    		forDeletion = new ArrayList<Long>();
    	} else {
    		forDeletion.clear();
    	}
    }
    
    public void onRowSelect(SelectEvent event) {
    	
    	String msgStr = "";
    	if(event.getObject() == null) {
    		LOG.error("Event object is NULL!!!!s " + event.getSource());
    	
    	}
    	
    	
    	msgStr = ((ItemRequest) event.getObject()).getId() + "";
        FacesMessage msg = new FacesMessage("Item Selected", msgStr);
        FacesContext.getCurrentInstance().addMessage(null, msg);
        
        LOG.debug(msgStr);
    }
    
	public void addItem(PartUnit partUnit, double quantity) {
		ItemRequest item = new ItemRequest();
		
		int itemNumber = irWrapperService.list().size() + 1;
		
		item.setItemNumber(itemNumber);
		item.setPartUnit(partUnit);
		item.setQuantity(quantity);
		
		irWrapperService.addItem(item);
		
		// re-initialize lazy model
		lazyModel = new LazyBreeDataModel<ItemRequest, ItemRequestWrapperService, Long>(irWrapperService, 0L);
	}
	
	public void remove() {
		
		Long id = selectedItem.getId();
		
		if(id > 0L) {
			forDeletion.add(id);
		}
		
		irWrapperService.removeItem(selectedItem);
	}
    
    public void removeItem(ItemRequest item) {
    	irWrapperService.removeItem(item);
    }

	public List<ItemRequest> getItemRequests() {
		List<ItemRequest> list = new ArrayList<ItemRequest>(irWrapperService.list());
		return list;
	}
	
	public void updateQuantity() {
		
		irWrapperService.updateQuantity(selectedItem, quantity);
	}

	/**
	 * @return the lazyModel
	 */
	public LazyDataModel<ItemRequest> getLazyModel() {
		return lazyModel;
	}

	/**
	 * @param lazyModel the lazyModel to set
	 */
	public void setLazyModel(LazyDataModel<ItemRequest> lazyModel) {
		this.lazyModel = lazyModel;
	}

	/**
	 * @return the selectedItem
	 */
	public ItemRequest getSelectedItem() {
		return selectedItem;
	}

	/**
	 * @param selectedItem the selectedItem to set
	 */
	public void setSelectedItem(ItemRequest selectedItem) {
		this.selectedItem = selectedItem;
		
		if(selectedItem != null) {
			this.quantity = selectedItem.getQuantity();
		}
	}

	/**
	 * @return the userSession
	 */
	public UserSessionData getUserSession() {
		return userSession;
	}

	/**
	 * @param userSession the userSession to set
	 */
	public void setUserSession(UserSessionData userSession) {
		this.userSession = userSession;
	}

	/**
	 * @return the quantity
	 */
	public double getQuantity() {
		return quantity;
	}

	/**
	 * @param quantity the quantity to set
	 */
	public void setQuantity(double quantity) {
		this.quantity = quantity;
	}

	/**
	 * @return the forDeletion
	 */
	public List<Long> getForDeletion() {
		return forDeletion;
	}
}
